//接口包配置 interface.html
app.controller('interfaceCtrl', function ($scope, $http, $location) {
  //页面通用初始化
  pageInit('interface');
  // 脱机
  $scope.isOffline = isOffline;
  //左侧导航用
  menuInit($scope, $http);
  $scope.menuIndex = 3.1;

  init();

  function dataHandle(rowData) {
    var interfaceTypeListIndex = [{
      index: 1,
      id: 'FSK',
      name: '全面风险',
      include: []
    }, {
      index: 2,
      id: 'CRK',
      name: '信用评级',
      include: []
    }, {
      index: 3,
      id: 'NK',
      name: '内控4.0',
      include: []
    }];

    for (var i in rowData) {
      var interfaceTypeName = rowData[i].interfaceTypeName;
      for (var j in rowData[i].infoList) {
        var productId = parseInt(rowData[i].infoList[j].productId);
        var include = interfaceTypeListIndex[productId - 1].include;
        if (include.indexOf(parseInt(i)) == -1) {
          include.push(parseInt(i));
        }
      }
    }

    return interfaceTypeListIndex;
  }

  //获取/刷新接口类型和接口列表
  function init() {
    $http.get('/deploy/interface/init').then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          var rowData = message.data
          $scope.interfaceTypeList = rowData;
          $scope.interfaceTypeListIndex = dataHandle(rowData);
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  //获取/刷新接口类型列表
  function initTypes() {
    $http.get('/deploy/interface/add').then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $scope.addTypeList = message.data;
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  //模块新增接口类别
  $('#interface-dialog').on('show.bs.modal', initTypes);

  $scope.addFnc = function (addTypeList) {
    var types = [];
    for (var i = 0; i < addTypeList.length; i++)
      if (addTypeList[i].checked) types.push(addTypeList[i].interfaceTypeId);
    if (types.length == 0) {
      $.showNotice("请正确选择接口类别", "danger");
      return;
    }
    var postData = {
      types: types
    };

    //执行添加
    $http.post('/deploy/interface/add', postData).then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          init();
          initTypes();
          $.showNotice("操作成功", "success");
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });

  };

  // 改变状态即提交
  $scope.checkedChange = function () {
    var rowData = $scope.interfaceTypeList;
    var postData = [];
    for (var i = 0; i < rowData.length; i++) {
      for (var j = 0; j < rowData[i].infoList.length; j++) {
        if (rowData[i].infoList[j].checked)
          postData.push(rowData[i].infoList[j].interfaceVersion);
      }
    }

    $http.post('/deploy/interface/submit', postData).then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          // 操作成功
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

  // 提交
  $scope.submitFnc = function () {

    if ($scope.loadingNext) return;
    $scope.loadingNext = true;
    var rowData = $scope.interfaceTypeList;

    var postData = [];
    for (var i = 0; i < rowData.length; i++) {
      for (var j = 0; j < rowData[i].infoList.length; j++) {
        if (rowData[i].infoList[j].checked)
          postData.push(rowData[i].infoList[j].interfaceVersion);
      }
    }

    $http.post('/deploy/interface/submit', postData).then(function (response) {
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          //init();
          // 脱机版
          if (isOffline) {
            $location.path('platform');
            return;
          }
          $location.path('installInterface');
        } else {
          $scope.loadingNext = false;
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.loadingNext = false;
      $.showNotice(response.status + " error：" + response.data.message, "danger");
    });
  }

});